package edu.friday.common.security.handle;

import com.alibaba.fastjson.JSON;
import edu.friday.common.constant.HttpStatus;
import edu.friday.common.result.RestResult;
import edu.friday.utils.StringUtils;
import edu.friday.utils.http.ServletUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import org.springframework.security.core.AuthenticationException;

import java.io.IOException;
import java.io.Serializable;


@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable{
    private static final long serialVersionUID = -88946546516513331L;


    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
            throws IOException {
        int code = HttpStatus.UNAUTHORIZED;
        String message = StringUtils.format("请求访问:{},认证失败",
                request.getRequestURI());
        ServletUtils.renderString(response, JSON.toJSONString(RestResult.error(code, message)));
    }
}
